import request from '@/utils/request.js'
import { useTokenStore } from '@/stores/token.js'

// 文章分类列表查询
export const articleCategoryListService = () => {
  // const tokenStore = useTokenStore()
  // // 在pinia中定义的响应式数据不需要.value
  // return request.get('/category', { headers: { 'Authorization': tokenStore.token } })
  return request.get('/category')
}

// 文章分类添加
export const articleCategoryAddService = (categoryData) => {
  return request.post('/category', categoryData)
}

// 文章分类修改
export const articleCategoryUpdateService = (categoryData) => {
  return request.put('/category', categoryData)
}

// 文章分类删除
export const articleCategoryDeleteService = (id) => {
  return request.delete('/category?id=' + id)
}

// 文章列表查询
export const articleListService = (params) => {
  return request.get('/article', { params: params })
}

//文章添加
export const articleAddService = (articleData) => {
  return request.post('/article', articleData)
}

//文章更新
export const articleUpdateService = (articleData) => {
  return request.put('/article', articleData)
}

// 文章删除
export const articleDeleteService = (id) => {
  return request.delete('/article?id=' + id)
}

// 全部文章
export const articleAllService = (params) => {
  return request.get('/article/all', { params: params })
}

// 全部分类
export const articleCategoryAllService = () => {
  return request.get('/category/all')
}

// 文章名称搜索
export const articleSearchService = (params) => {
  return request.get('/article/search', { params: params })
}

// 文章详情
export const articleDetailService = (id) => {
  return request.get('/article/detail?id=' + id)
}

// 标签
// 获取标签列表
export const AllTagService = () => {
  return request.get('/tag/all')
}

// 标签种类添加
export const TagAddService = (tagData) => {
  return request.post('/tag', tagData)
}

// 标签种类修改
export const TagUpdateService = (tagData) => {
  return request.put('/tag', tagData)
}

// 标签种类删除
export const TagDeleteService = (tagId) => {
  return request.delete('/tag?tagId=' + tagId)
}

// 给文章添加标签
export const articleTagAddService = (articleId, tagIds) => {
  // 确保tagIds是数组并且有值
  if (!tagIds || !tagIds.length) return Promise.reject(new Error('标签ID不能为空'));
  
  // 如果tagIds是数组，将其转换为逗号分隔的字符串
  const tagIdsStr = Array.isArray(tagIds) ? tagIds.join(',') : tagIds;
  
  return request.post('/articleTag/'+articleId+'/tags?tagIds='+tagIdsStr);
}

// 给文章删除标签
export const articleTagDeleteService = (articleId, tagIds) => {
  // 确保tagIds是数组并且有值
  if (!tagIds || !tagIds.length) return Promise.reject(new Error('标签ID不能为空'));
  
  // 如果tagIds是数组，将其转换为逗号分隔的字符串
  const tagIdsStr = Array.isArray(tagIds) ? tagIds.join(',') : tagIds;
  
  return request.delete('/articleTag/'+articleId+'/tags?tagIds='+tagIdsStr);
}

// 删除文章的所有标签
export const articleTagDeleteAllService = (articleId) => {
  return request.delete('/articleTag/'+articleId+'/tags/all');
}

// 给文章修改标签
export const articleTagUpdateService = async (articleId, tagIds) => {
   // 确保tagIds是数组并且有值
  if (!tagIds || !tagIds.length)     
    return request.put('/articleTag/'+articleId+'/tags');

   // 如果tagIds是数组，将其转换为逗号分隔的字符串
  const tagIdsStr = Array.isArray(tagIds) ? tagIds.join(',') : tagIds;
  
  return request.put('/articleTag/'+articleId+'/tags?tagIds='+tagIdsStr);
}

// 根据标签名或标签ID搜索文章
export const articleSearchByTagService = ({ tagNames, tagIds }) => {
  // 创建查询参数对象
  const params = new URLSearchParams();
  // 处理 tagName 数组（标签名）
  if (tagNames && tagNames.length > 0) {
    tagNames.forEach(name => params.append('tagName', name));
  }
  // 处理 tagId 数组（标签ID）
  if (tagIds && tagIds.length > 0) {
    tagIds.forEach(id => params.append('tagId', id));
  }
  // 发送 GET 请求
  return request.get(`/articleTag/by-tag?${params.toString()}`);
};